package day16.xianchengdemo.fight;

import java.util.*;
import java.util.concurrent.CountDownLatch;

import static java.util.Collection.*;

//100个人抢两百个红包，160个小红包金额在1-30之间，40个大红包金额在31-100之间，请求设计一个程序，模拟抢红包的过程，说谁抢到了红包，并计算抢到红包的总金额,降序输出。
public class test
{

    private static List<Integer> redBag=new ArrayList<Integer>();
    private static List<Employee_redbag> employees=new ArrayList<>();

    public static void main(String[] args) throws InterruptedException {
        Random r=new Random();
        //构建两百个红包
        for(int i=0;i<160;i++)
        {
         redBag.add(r.nextInt(31)+1);
        }
        for(int i=0;i<40;i++)
        {
            redBag.add(r.nextInt(70)+31);
        }

        int numberOfEmployees = 100;
        CountDownLatch latch = new CountDownLatch(numberOfEmployees);

        //模拟100个人（100个线程）
        for(int i=1;i<=100;i++)
        {
            Employee_redbag temp=new Employee_redbag(redBag,latch);
            employees.add(temp);
            Thread t1=new Thread(temp);
             t1.start();
        }
      latch.await();//会等到latch数量=0开始做下面的

        System.out.println("***************************");

        Collections.sort(employees,new Comparator<Employee_redbag>()
        {
            public int compare(Employee_redbag a,Employee_redbag b)
            {
                return b.getSum()-a.getSum();
            }
        });



       for(Employee_redbag e:employees)
       {
           System.out.println(e.getName()+"抢到了"+e.getSum()+"金额的红包");
       }






    }
}
